package com.fzrskj.cloud.free.user.utils;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.core.enums.MessageSendWayEnum;
import com.fzrskj.cloud.free.core.enums.SysUserGroupCodeEnum;
import com.fzrskj.cloud.free.core.tool.CEncryptTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.ConfigPropUtils;
import com.fzrskj.cloud.free.dic.dao.DicParamDao;
import com.fzrskj.cloud.free.sys.bean.User;
import com.fzrskj.cloud.free.sys.utils.CUserUtils;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.bean.UserAccountView;
import com.fzrskj.cloud.free.user.dao.UserAccountDao;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.StrTool;
import org.apache.commons.collections.map.HashedMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserAccountUtils extends CObject{

	//to encode user password
	public static String encodePassword(String password){
		String type = ConfigPropUtils.getValue("userPasswordType", "1");
		if ("1".equals(type)) {
			return CEncryptTool.md5Encode(password+"huilin");
		} else if ("2".equals(type)){
			return CEncryptTool.md5Encode(CEncryptTool.md5Encode(password)+"huilin");
		} else if ("3".equals(type)){
			return CEncryptTool.md5Encode(password);
		}
		return password;
	}

	public static String encodePasswordFromMd5(String passwordMd5) {
		String type = ConfigPropUtils.getValue("userPasswordType", "1");
		if ("1".equals(type)) {
			return "";
		} else if ("2".equals(type)){
			return CEncryptTool.md5Encode(passwordMd5+"huilin");
		} else if ("3".equals(type)){
			return passwordMd5;
		}
		return passwordMd5;
	}

	/**
	 * to verify user account with password
	 * @param act
	 * @param pass
	 * @return not exist=-2; invalid password=-1; not active=0; normal = 1; suspend = 2 (reserved)
	 */
	private int verifyLogin(UserAccount act, String pass){
		if (act==null){
			this.setErrCode(SysErr.USER_NOT_EXISTS);
			return -2;
		}
		if (!act.getPassword().equals(encodePassword(pass))){
			if (!act.getPassword().equals(CEncryptTool.md5Encode(pass))){
				this.setErrCode(SysErr.USER_PASSWORD_ERR);
				return -1;
			}
		}
		if (!act.getStatus().equals(CConsts.ACCOUNT_NORMAL)){
			this.setErrCode(SysErr.USER_INACTIVE);
			return 0;
		}
		this.setErrCode(SysErr.CODE_SUCCESS);
		return 1;
	}

	/**
	 * user login by userName
	 * @param userName user register account
	 * @param pass user's password
	 * @param visitIp visit ip address
	 * @return null for false, otherwise return UserAccount object
	 */
	public UserAccount userLogin(String userName, String pass, String visitIp){
		UserAccountDao dao=new UserAccountDao();
		UserAccount ret = dao.queryUserAccount(userName);
		String code = dao.getErrCode();
		dao.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.setErrCode(code);
			return null;
		}
		if (verifyLogin(ret, pass)>0) {
			return ret;
		}

		return null;
	}

	//查询会员是否已注册
	public UserAccount isRegister(String userName){
		UserAccountDao dao=new UserAccountDao();
		UserAccount ret = dao.queryUserAccount(userName);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	// 查询邮箱是否已注册
	public UserAccount isRegisterEmail(String email) {
		UserAccountDao dao = new UserAccountDao();
		UserAccount ret = dao.queryUserAccountEmail(email);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 查询邮箱除指定用户外是否被注册使用.
	 * @param email 邮箱账号.
	 * @param ignoreUserId 需要忽略的用户ID.
	 * @return UserAccountView.
	 */
	public UserAccountView isRegisterEmail(String email, Integer ignoreUserId) {
		UserAccountDao dao = new UserAccountDao();
		UserAccountView ret = dao.isRegisterMail(email, ignoreUserId);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 查询手机号除指定用户外是否被注册使用.
	 * @param mobile 手机号.
	 * @param ignoreUserId 需要忽略的用户ID.
	 * @return isRegisterMobile.
	 */
	public UserAccountView isRegisterMobile(String mobile, Integer ignoreUserId) {
		UserAccountDao dao = new UserAccountDao();
		UserAccountView ret = dao.isRegisterMobile(mobile, ignoreUserId);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	public int userRegister(String userName, String email, String mobile, String password, Integer userType){
		return userRegister(userName,email,mobile,password,userType,false,"",1);
	}
	public int userRegister(String userName, String email, String mobile, String password, Integer userType,Boolean isVilidate){
		return userRegister(userName,email,mobile,password,userType,isVilidate,"",1);
	}
	/**new user register
	 * @param email email of registration
	 * @param mobile mobile of registration
	 * @param password
	 * @return if success, return user record id, otherwise return <=0
	 */
	public int userRegister(String userName, String email, String mobile, String password, Integer userType,Boolean isVilidate,String openId,int loginType){
		DicParamDao dicParamDao = new DicParamDao();
		String getParamValue = dicParamDao.getParamValue("userLevel");
		String sendWay = dicParamDao.getParamValue(DicParamEnum.CODE_LOGIN_WAY.getVal());
		String code1 = dicParamDao.getErrCode();
		dicParamDao.dispose();
		if (!code1.equals(SysErr.CODE_SUCCESS)){
			this.setErrCode(code1);
			return -1;
		}
		UserAccountDao dao=new UserAccountDao();
		UserAccount ret = dao.queryUserAccount(userName);
		String code = dao.getErrCode();
		dao.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.setErrCode(code);
			return -1;
		}
		if (ret!=null){
			this.setErrCode(SysErr.USER_EXISTS);
			return 0;
		}

		//验证邮箱是否已注册
		dao=new UserAccountDao();
		if(MessageSendWayEnum.MAIL.equals(sendWay)){
			ret = dao.queryUserAccountEmail(email);
			code = dao.getErrCode();
			if (!code.equals(SysErr.CODE_SUCCESS)){
				this.setErrCode(code);
				return -1;
			}
			if (ret!=null){
				this.setErrCode("MAIL_EXISTS", "该邮箱已被注册");
				return 0;
			}
		}

		//验证手机号是否已注册
		if(MessageSendWayEnum.SMS.equals(sendWay)) {
			UserAccountView view = dao.queryUserAccountViewByMobile(mobile);
			code = dao.getErrCode();
			if (!code.equals(SysErr.CODE_SUCCESS)) {
				this.setErrCode(code);
				return -1;
			}
			if (view != null) {
				this.setErrCode("MOBILE_EXISTS", "该手机号已被注册");
				return 0;
			}
		}
		UserAccount u = new UserAccount();
		u.setUserName(userName);
		u.setPassword(encodePassword(password));
		u.setLevelCode(getParamValue);
		u.setOpenId(openId);
		u.setLoginType(loginType);
		if(isVilidate){
			u.setStatus("Y");
		}
		dao=new UserAccountDao();
		int rid = dao.addUserAccount(u);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		if (rid>0){
			UserAccount uc=queryAccountByName(userName);
			if(uc!=null && this.getErrCode().equals(SysErr.CODE_SUCCESS)){
				UserAccountDataUtils utls = new UserAccountDataUtils();
				utls.addUserAccountData(userName, email, mobile, uc.getUserId(), userType);
			}

		}
		return rid;
	}


	/**
	 * 第三方账号未绑定和注册，进行添加注册新账号
	 * @return
	 */
	public int addThridAccounRegister(String openId,String loginType){
		return 1;
	}

	/**
	 * to active user account
	 * @param uid
	 * @param randomCode
	 * @return true for active success, false active failure
	 */
	public boolean activeUserAccount(int uid, String randomCode){
		UserAccountDao dao=new UserAccountDao();
		UserAccount u = dao.queryUserAccount(uid);
		String code = dao.getErrCode();
		dao.dispose();
		//system error
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.setErrCode(code);
			return false;
		}
		//user not exists
		if (u==null){
			this.setErrCode(SysErr.USER_NOT_EXISTS);
			return false;
		}
		if (u.getStatus().equals(CConsts.ACCOUNT_NORMAL)){
			this.setErrCode(AppCode.USER_ACTIVED);
			return false;
		}
		//to verify random code
		if (!randomCode.equals(u.getRandomCode())){
			 this.setErrCode(AppCode.RANDOMCODE_ERROR);
			 return false;
		}

		dao=new UserAccountDao();
		boolean ret = dao.activeUserAccount(uid);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * to update user password
	 * @param userName
	 * @param oldPass
	 * @param newPass
	 * @return
	 */
	public int updateUserPassword(String userName, String oldPass, String newPass){
		UserAccountDao dao=new UserAccountDao();
		UserAccount u = dao.queryUserAccount(userName);
		String code = dao.getErrCode();
		dao.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return -1;
		}
		if (u==null){
			this.setErrCode(SysErr.USER_NOT_EXISTS);
			return 0;
		}
		oldPass = encodePassword(oldPass);
		if (!oldPass.equals(u.getPassword())){
			this.setErrCode(SysErr.USER_PASSWORD_ERR);
			return -1;
		}
		//buid to update value field map
		newPass = encodePassword(newPass);
		Map<String,Object> values = new HashMap<String, Object>();
		values.put("passWord", newPass);
		//build condition field map
		Map<String,Object> cdt = new HashMap<String, Object>();
		cdt.put("userName", userName);

		dao=new UserAccountDao();
		int ret = dao.modUserAccount(values, cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * to reset user password
	 * @param userName
	 * @param newPass
	 * @return
	 */
	public int resetUserPassword(String userName, String newPass){
		newPass = encodePassword(newPass);
		Map<String,Object> values = new HashMap<String, Object>();
		values.put("password", newPass);
		//build condition field map
		Map<String,Object> cdt = new HashMap<String, Object>();
		cdt.put("userName", userName);

		UserAccountDao dao=new UserAccountDao();
		int ret = dao.modUserAccount(values, cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 根据userId与userName删除用户
	 * @param userId
	 * @param userName
	 * @return
	 */
	public int deleteUserAccountByUserNameAndUserId(int userId,String userName){
		UserAccountDao dao=new UserAccountDao();
		int ret = dao.deleteUserAccountByUserNameAndUserId(userId, userName);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 根据userName与userId修改状态
	 * @param userName
	 * @param rid
	 * @return
	 */
	public int uptUserAccountState(String userName,int rid){
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("id", rid);
		value.put("userName", userName);
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("randomCode", "管理员注册");
		cdt.put("status", "Y");

		UserAccountDao dao=new UserAccountDao();
		int ret = dao.uptUserAccountState(cdt, value);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 根据userName与userId修改状态
	 * @param userName
	 * @param rid
	 * @param status
	 * @return
	 */
	public int uptUserAccountState(String userName,int rid,String status){
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("userId", rid);
		value.put("userName", userName);
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("status", status);

		UserAccountDao dao=new UserAccountDao();
		int ret = dao.uptUserAccountState(cdt, value);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public UserAccount queryAccountByName(String userName){
		return this.queryAccountByName(userName, CloudContextUtils.getAgentId());
	}

	/**
	 * 根据userName查询对象
	 * @param userName
	 * @return
	 */
	public UserAccount queryAccountByName(String userName, Integer agentId){
		UserAccountDao dao=new UserAccountDao();
		UserAccount queryAccountByName = dao.queryAccountByName(userName, agentId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryAccountByName;
	}

	/**
	 * 查询代理商账号信息
	 * @param apiAccessKey user account.
	 * @return agent account info.
	 */
	public UserAccountView queryAgentByAccessKey(String apiAccessKey) {
		UserAccountDao dao=new UserAccountDao();
		UserAccountView queryAccountByName = dao.queryAgentByAccessKey(apiAccessKey);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryAccountByName;
	}

	/**
	 * 根据userName更新Code
	 * @param userName
	 * @return
	 */
	public int uptAccountByCode(String userName,String randomNum){
		UserAccountDao dao=new UserAccountDao();
		int ret = dao.uptAccountByCode(userName, randomNum);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public int uptAccountOpenIdByuserId(int userId,String openId,int loginType){
		UserAccountDao dao=new UserAccountDao();
		int ret = dao.uptAccountOpenIdByuserId(userId,openId,loginType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 根据userName与randomCode查找对象
	 * @param userName
	 * @param checkcode
	 * @return
	 */
	public UserAccount queryUserNameAndCode(String userName,String checkcode){
		UserAccountDao dao=new UserAccountDao();
		UserAccount queryUserNameAndCode = dao.queryUserNameAndCode(userName, checkcode);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryUserNameAndCode;
	}

	//to query user account for rows
    public int queryUserAccountView(String account, String email, String mobile, String levelCode, String bstartTime, String bendDate) {
		Map<String,Object> cdt = new HashMap<>();
		if (!StrTool.isBlankStr(account)) {
			cdt.put("a.userName", account);
		}
		if (!StrTool.isBlankStr(email)) {
			cdt.put("d.email", email);
		}
		if (!StrTool.isBlankStr(mobile)) {
			cdt.put("d.mobile", mobile);
		}
		if (!StrTool.isBlankStr(levelCode)) {
			cdt.put("a.levelCode", levelCode);
		}

		if (!StrTool.isBlankStr(bstartTime)) {
			cdt.put("a.regTime >", bstartTime);
		}

		if (!StrTool.isBlankStr(bendDate)) {
			cdt.put("a.regTime <", bendDate);
		}

        UserAccountDao dao = new UserAccountDao();
        int rows = dao.queryUserAccount(cdt);
        String code = dao.getErrCode();
        this.setErrCode(code);
        dao.dispose();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return -1;
        }
		return rows;
	}

	public int queryUserAccountView(Map<String,Object> cdt){
		UserAccountDao dao=new UserAccountDao();
		int rows = dao.queryUserAccountAdmin(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return -1;
		}
		return rows;
	}

	//to query user account for account list
	public List<UserAccountView> queryUserAccountView(String account, String email, String mobile, String levelCode, Integer certify, String bstartTime, String bendDate, int page, int pageSize){
		Map<String,Object> cdt = new HashMap<String, Object>();
		if (!StrTool.isBlankStr(account)) {
			cdt.put("a.userName", account);
		}
		if (!StrTool.isBlankStr(email)) {
			cdt.put("d.email", email);
		}
		if (!StrTool.isBlankStr(mobile)) {
			cdt.put("d.mobile", mobile);
		}
		if (!StrTool.isBlankStr(levelCode)) {
			cdt.put("a.levelCode", levelCode);
		}
		if (certify > -1) {
			cdt.put("d.isCertified", certify);
		}

        if (!StrTool.isBlankStr(bstartTime)) {
            cdt.put("a.regTime >", bstartTime);
        }

        if (!StrTool.isBlankStr(bendDate)) {
            cdt.put("a.regTime <", bendDate);
        }

		UserAccountDao dao=new UserAccountDao();
		List<UserAccountView> uas =  dao.queryUserAccountList(cdt, page, pageSize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return uas;
	}

	public List<UserAccountView> queryUserAccountView(Map<String,Object> cdt,  int page, int pageSize){
		UserAccountDao dao=new UserAccountDao();
		List<UserAccountView> uas =  dao.qryUserAccountListAdmin(cdt, page, pageSize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return uas;
	}

	//query user account detail by user record id
	public UserAccountView queryUserAccountView(int rid){
		UserAccountDao dao=new UserAccountDao();
		UserAccountView ret = dao.queryUserAccountView(rid);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public UserAccountView queryUserAccountViewByUid(int uid){
		return this.queryUserAccountViewByUidAppointAgent(uid, CloudContextUtils.getAgentId());
	}

	//query user account detail by user record id
	public UserAccountView queryUserAccountViewByUidAppointAgent(int uid, Integer agentId){
		UserAccountDao dao=new UserAccountDao();
		UserAccountView ret = dao.queryUserAccountViewByUidAppointAgent(uid, agentId);

		if(ret==null){
			String code = dao.getErrCode();
			this.setErrCode(code);
			dao.dispose();
			return null;
		}

		//自定义产品
		UserAccountView retCountDiy = dao.queryUserDiyAccountView(ret.getUserId());
		ret.setUserCountDiy(retCountDiy.getUserCountDiy());
		ret.setUserCountDiyExpire(retCountDiy.getUserCountDiyExpire());
		ret.setUserCountDiyExpiring(retCountDiy.getUserCountDiyExpiring());

		//客服
		if (ret.getCrmId() > 0) {
			User crmUser = CUserUtils.getUser(ret.getCrmId());
			if (null != crmUser) {
				ret.setCrmName(crmUser.getNick());
			}
		}

		//技术
		if (ret.getCsstId() > 0) {
			User csstUser = CUserUtils.getUser(ret.getCsstId());
			if (null != csstUser) {
				ret.setCsstName(csstUser.getNick());
			}
		}

		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public UserAccountView queryUserAccountWithoutAgent(Integer userId) {
		UserAccountDao dao=new UserAccountDao();
		UserAccountView ret = dao.queryUserAccountWithoutAgent(userId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public static UserAccountView getUserAccountWithoutAgent(Integer userId) {
		UserAccountUtils accountUtils = new UserAccountUtils();
		return accountUtils.queryUserAccountWithoutAgent(userId);
	}

	// query user account detail by user record id
	public UserAccount queryUserAccountByUid(int uid) {
		UserAccountDao dao = new UserAccountDao();
		UserAccount ret = dao.queryUserAccount(uid);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public UserAccount queryUserAccountById(int id){
		UserAccountDao dao = new UserAccountDao();
		UserAccount ret = dao.queryUserAccountById(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	//query user account detail by user email
	public UserAccountView queryUserAccountView(String email){
		UserAccountDao dao=new UserAccountDao();
		UserAccountView ret = dao.queryUserAccountView(email);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 通过手机账号获取会员信息.
	 * @param mobile mobile.
	 * @return
	 */
	public UserAccountView queryUserAccountViewByMobile(String mobile) {
		UserAccountDao dao=new UserAccountDao();
		UserAccountView ret = dao.queryUserAccountViewByMobile(mobile);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public UserAccountView qryUserAccountViewByOpenId(String openId){
		UserAccountDao dao=new UserAccountDao();
		UserAccountView ret = dao.qryUserAccountViewByOpenId(openId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 根据用户名修改会员等级
	 * @param userName
	 * @param userId
	 * @return
	 */
	public int uptAccountLevel(int userId,String userName,String levelCode){
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("userName", userName);
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("levelCode", levelCode);

		UserAccountDao dao=new UserAccountDao();
		int ret = dao.uptUserAccountState(cdt, value);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();

		if(ret>0) {
			UserLogUtils.addLog(this, userId, "uptAccountLevel", "更改会员等级，会员名【" + userName + "】，等级代码【" + levelCode + "】");
		}
		return ret;
	}

	public static UserAccountView getUserAccountView(int uid){
		UserAccountUtils utls = new UserAccountUtils();
		UserAccountView uav = utls.queryUserAccountViewByUid(uid);
		return uav;
	}



	public int getUserAccount(Map<String, Object> cdt){
		UserAccountDao dao=new UserAccountDao();
		int ret = dao.getUserAccount(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 更新余额
	 * @param userId
	 * @param userName
	 * @param balance
	 * @return
	 */
	public int updateBlance(int userId,String userName,double balance, Integer agentId){
		UserAccountDao dao=new UserAccountDao();
		int ret=dao.updateBlance(userId,userName,balance, agentId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 更新用户账号.
	 * @param values 更新信息.
	 * @param cdt 更新条件
	 * @return affect line number.
	 */
	public int modUserAccount(Map<String,Object> values, Map<String,Object> cdt){
		UserAccountDao dao = new UserAccountDao();
		int ret = dao.modUserAccount(values, cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		if (ret==0) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	/**
	 * 自动分配客服和技术.
	 * @param userId 用户ID.
	 */
	public void allotCrmAndCSST(Integer userId) {
		UserAccount account = this.queryUserAccountByUid(userId);
		if (null == account) {
			return;
		}

		//分配客服
		this.allotCrm(account);

		//分配
		this.allotCsst(account);
	}

	/**
	 * 自动分配客服.
	 * @param account account info.
	 */
	public void allotCrm(UserAccount account) {
		//获取用户最小的客服ID.
		if (0 == account.getCrmId()) {
			User user = CUserUtils.getAllotUser(SysUserGroupCodeEnum.CRM.getCode());
			if (null != user) {
				Map<String, Object> update = new HashedMap();
				update.put("crmId", user.getAccountId());

				Map<String, Object> cdt = new HashedMap();
				cdt.put("id", account.getId());

				this.modUserAccount(update, cdt);
			}
		}
	}



	/**
	 * 自动分配售后技术.
	 * @param account account.
	 */
	public void allotCsst(UserAccount account) {
		//获取用户最小的客服ID.
		if (0 == account.getCsstId()) {
			User user = CUserUtils.getAllotUser(SysUserGroupCodeEnum.CSST.getCode());
			if (null != user) {
				Map<String, Object> update = new HashedMap();
				update.put("csstId", user.getAccountId());

				Map<String, Object> cdt = new HashedMap();
				cdt.put("id", account.getId());

				this.modUserAccount(update, cdt);
			}
		}
	}

    public static Integer getTopAgentUserId(int userId) {
        Integer topUserId = 0;
        do {
            UserAccountDao dao = new UserAccountDao();
            UserAccount ret = dao.getUserAccountWithoutAgent(userId);
            dao.dispose();
            if (ret == null) {
				topUserId =  0;
                break;
            }
            if (ret.getAgentId() == 0){
                topUserId =  userId;
                break;
            }
            return getTopAgentUserId(ret.getAgentId());
        } while (false);
        return topUserId;
    }

	public static void main(String[] args) {
		UserAccount userAccount = new UserAccount();
		UserAccountUtils uaUtils = new UserAccountUtils();
		userAccount = uaUtils.isRegister("123131");
		UserAccountView accountView = uaUtils.qryUserAccountViewByOpenId("1241");
		String ss = "11";

	}
}